<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use App\Http\Requests;
use DB;

class ConfigController extends CommonController
{
    
    public function index()
    {   
        //获取  config数据表的信息
        $list = DB::table("config")->get();
        return view("admin.config.index",['list'=>$list]);
    }
    
    public function edit($id)
    {   //加载 config数据标的的信息
        $ss = DB::table("config")->get();
        return view("admin.config.add",['ss'=>$ss]);
    }

    public function update(Request $request,$id)
    {
        if(!empty($request->logo)){
            $aa = DB::table("config")->get();
            foreach ($aa as $k) {
                unlink("./uploads/".$k->logo);
            }
            
    	if($request->hasFile('logo')) {
            //获取上文件信息
            $file = $request->file('logo');
            //获取上传文件的后缀名
            $ext = $file->getClientOriginalExtension();
            //随机一个新的文件名
            $filename = time().rand(1000,9999).".".$ext;
            //执行文件移动
            $newfile = $file->move("./uploads/",$filename);
            
            $fimg = Image::make("./uploads/".$filename)->resize(50,50);
            $fimg->save("./uploads/".$filename);

            //获取修改后的数据表信息  发送到网站配置 index 页面 
            $ss = ["webname"=>$request->webname,"keywords"=>$request->keywords,"logo"=>$filename,"copy"=>$request->copy,"status"=>$request->status];
             
             DB::table('config')->update($ss);
               
        }
            $list = DB::table("config")->get();

            return view("admin.config.index",['list'=>$list]);
    }else{
            $ss = ["webname"=>$request->webname,"keywords"=>$request->keywords,"copy"=>$request->copy,"status"=>$request->status];
             
             DB::table('config')->update($ss);
            $list = DB::table("config")->get();
            
           return view("admin.config.index",['list'=>$list]);
       }   
    }

}
